package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.newJz.data.QQListView;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by fanxs11 on 16/4/26.
 */
public class BeiDan_RSPF_Adapter extends BeiDanAdapter{

    protected int leftBallColor;

    public BeiDan_RSPF_Adapter(Context context, List<BeiDanBean> infos, QQListView listView, OnBeiDanCheckBallListener listener) {
        super(context, infos, listView, listener);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder holder;
        final BeiDanChildBean info = infos.get(groupPosition).childBeen.get(childPosition);
//
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.beidan_list_child, null);
            holder.matchNum_tv = convertView.findViewById(R.id.beidan_list_matchNum_tv);
            holder.matchName_tv = convertView.findViewById(R.id.beidan_list_matchName_tv);
            holder.endSaleTime_tv = convertView.findViewById(R.id.beidan_list_endSaleTime_tv);
            holder.home_tv = convertView.findViewById(R.id.beidan_list_home);
            holder.away_tv = convertView.findViewById(R.id.beidan_list_away);
            holder.rq_tv = convertView.findViewById(R.id.beidan_list_rq);
            holder.sheng_cb = convertView.findViewById(R.id.beidan_list_sheng_cb);
            holder.ping_cb = convertView.findViewById(R.id.beidan_list_ping_cb);
            holder.fu_cb = convertView.findViewById(R.id.beidan_list_fu_cb);

            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }

        holder.matchNum_tv.setText(info.getMatchNum()+"");
        holder.matchName_tv.setText(info.getMatchName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        String endTimeStr = simpleDateFormat.format(new Date(info.getEndSaleTime()));
        holder.endSaleTime_tv.setText(endTimeStr+"截止");
        holder.home_tv.setText(info.getHomeTeam());
        holder.away_tv.setText(info.getAwayTeam());

        int leftBall = 0;
        try {
            leftBall = Integer.parseInt(info.getHandicap());
        } catch (NumberFormatException e) {

        }
        if (leftBall > 0) {
            leftBallColor = context.getResources().getColor(R.color.color_FF4258);
        } else if (leftBall == 0) {
            leftBallColor = context.getResources().getColor(R.color.color_4EA5FF);
        } else {
            leftBallColor = context.getResources().getColor(R.color.color_47965a);
        }
        holder.rq_tv.setBackgroundColor(leftBallColor);
        holder.rq_tv.setText(info.getHandicap());

        holder.sheng_cb.setText("让胜\n"+info.getLetvs_3());
        holder.ping_cb.setText("让平\n"+info.getLetvs_1());
        holder.fu_cb.setText("让负\n"+info.getLetvs_0());

        holder.sheng_cb.setChecked(info.isSheng);
        holder.ping_cb.setChecked(info.isPing);
        holder.fu_cb.setChecked(info.isFu);
        jiSuanChangCi();

        holder.sheng_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isSheng = !info.isSheng;
                if (info.isSheng) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        holder.ping_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isPing = !info.isPing;
                if (info.isPing) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        holder.fu_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMax(info.checkNum, (CheckBox) view)) {
                    return;
                }
                info.isFu = !info.isFu;
                if (info.isFu) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                jiSuanChangCi();
            }
        });

        return convertView;
    }

    @Override
    protected void jiSuanChangCi() {
        changCi = 0;
        for (int i = 0; i < infos.size(); i++) {
            List<BeiDanChildBean> childBeens = infos.get(i).childBeen;
            for (int j = 0; j < childBeens.size(); j++) {
                if (childBeens.get(j).checkNum > 0) {
                    changCi++;
                }
            }
        }
        if (listener != null) {
            listener.onCheckBall(changCi);
        }
    }

    private class ChildViewHolder {
        TextView matchNum_tv;
        TextView matchName_tv;
        TextView endSaleTime_tv;
        TextView home_tv;
        TextView away_tv;
        TextView rq_tv;
        CheckBox sheng_cb;
        CheckBox ping_cb;
        CheckBox fu_cb;
    }

}
